package com.stx.service;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.stx.exception.CustomException;
import com.stx.mapper.RecordMapper;

import com.stx.pojo.Match;
import com.stx.pojo.Record;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.websocket.Session;
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class RecordService extends ServiceImpl<RecordMapper, Record> {

    @Autowired
    private RecordMapper recordMapper;

    public List<Record> getrecord() {
        return recordMapper.getrecord();
    }


    public void add(Record record) {
        //1.用户名一定要有，否则不让新增（后面需要用户名登录）
        if(record.getRecScore() == null ) {
            throw new CustomException("分数不能为空");
        }
        if(record.getRecDate() == null ) {
            record.setRecDate(LocalDateTime.now());
        }
        recordMapper.add(record);
    }

    public void update(Record record) {
        record.setRecDate(LocalDateTime.now());
        recordMapper.updateID(record);
    }

    public void delete(Integer id) {
        recordMapper.deleteID(id);
    }

    public void addScore(Integer session, Record record) {
        record.setPlaId(session);
        recordMapper.addScore(record);
    }

    public List<Record> findByPlayerId(int playerId ,int ruaId) {
        LambdaQueryWrapper<Record> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Record::getPlaId, playerId);
        wrapper.eq(Record::getRuaId, ruaId);
        wrapper.orderByDesc(Record::getRecScore);
        return recordMapper.selectList(wrapper);
    }

    public double calculateAverageScore(List<Record> list) {

        if (CollectionUtils.isEmpty(list)) {
            return 0.0;
        }

        if (list.size() < 3) {
            return list.stream().mapToDouble(Record::getRecScore).average().orElse(0.0);
        }

        list.remove(0);
        list.remove(list.size() - 1);

        return list.stream().mapToDouble(Record::getRecScore).average().orElse(0.0);
    }


   /* public void addscore(Session session, Record record) {
        //1.判断是否已经有了记录，如果有，则更新，如果没有，则新增
        Record record1 = recordMapper.getRecordBySession(session, record);
        if(record1!= null) {
            record.setRecId(record1.getRecId());
            record.setRecDate(LocalDateTime.now());
            recordMapper.updatescore(record);
                }else {
                    record.setRecDate(LocalDateTime.now());
                    recordMapper.addscore(session, record);
        }

        *//*recordMapper.addscore(session, record);*//*
    }*/

    public List<Record> listByJudger(Integer judeId) {
        LambdaQueryWrapper<Record> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Record::getJudId, judeId);
        return baseMapper.selectList(wrapper);
    }

    public Page<Record> find(Object plaName,Object judName,Integer number, Integer currentPage, Integer pageSize) {
        System.out.println("name=" + plaName);
        LambdaQueryWrapper<Record> wrapper = new LambdaQueryWrapper<>();
        //看plaName是否为空，为空就不执行
        wrapper.like(plaName!=null, Record::getPlaName, plaName);
        wrapper.like(judName!=null, Record::getJudName, judName);
        wrapper.like(number!=null, Record::getRuaNumber, number);
        //创建分页条件
        Page<Record> page = new Page<>(currentPage, pageSize);
        //执行分页查询（分页结果）
        Page<Record> resultPage = recordMapper.selectPage(page, wrapper);
        //当前页数据
        List<Record> records = resultPage.getRecords();
        //总记录数
        long total = resultPage.getTotal();
        //总页数
        long pages = resultPage.getPages();
        com.stx.common.Page<Record> pageFind = new com.stx.common.Page<>();
        pageFind.setRecords(records);
        pageFind.setTotal(total);
        pageFind.setPages(pages);
        return page;
    }
}
